<?php

namespace App\Domain\Address\Controllers;

use App\Domain\Address\Requests\updateAndcreatePost;
use App\Http\Controllers\Controller;
use App\Supports\Constant\ExceptionCode;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use App\Domain\Address\Services\AddressService;
use Illuminate\Pagination\LengthAwarePaginator;

class AddressController extends Controller
{
    public function tree(Request $request, AddressService $addressService)
    {
        $this->validate($request, [
            'regionId' => 'present|nullable',
            'regionLevel' => 'present|in:0,,1,2,3,4,5|nullable',
            'levelType' => 'required|in:0,1,2,3,4'
        ]);
        $params = [
            'regionId' => $request['regionId'],
            'level' => $request['regionLevel'],
            'levelType' => $request->input('levelType'),
        ];
        switch ($params['levelType']) {
            case '0':
                $data[] = $addressService->getAddress(0, 0);
                break;
            case '1'://当前节点和所有子节点
                if ($params['level'] == '0') {
                    return $this->success([], '此接口不可获取区划级别小于1所有区划', ExceptionCode::BAD_REQUEST_EXCEPTION_CODE);
                }
                $data[] = $addressService->getAddress($params['regionId'], $params['level']);
                break;
            case '2'://父节点和兄弟节点
                $data[] = $addressService->getBrother($params['regionId'], $params['level']);
                break;
            case '3'://当前节点和子节点
                if (empty($params['regionId']) || $params['regionId'] == '0') {
                    $data = $addressService->getSons($params['regionId'], $params['level'], 1);
                } else {
                    $data[] = $addressService->getAddressSon($params['regionId'], $params['level'], 1);
                }
                break;
            case '4'://当前节点
                $data[] = $addressService->getAddressTree($params['regionId'], $params['level'], 0);
                break;
        }
        return $this->success($data);
    }

    public function province(AddressService $addressService)
    {
        $data = $addressService->province();
        return $this->success($data);
    }

    //添加修改
    public function setAddress(updateAndcreatePost $request, AddressService $addressService)
    {
        $param = $request->validated();
        $data = $addressService->setAddress($param);
        return $this->success($data);
    }

    //删除
    public function del(Request $request, AddressService $addressService)
    {
        $res = $addressService->del($request['id']);
        return $this->success($res);
    }

    //列表
    public function list(Request $request, AddressService $addressService)
    {
        $this->validate($request, [
            'regionId' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'regionId' => $request['regionId'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $addressPaginator = $addressService->list($param);
        $addressPaginator = new LengthAwarePaginator($addressPaginator->items(), $addressPaginator->total(), $addressPaginator->perPage(), $addressPaginator->currentPage());
        //格式化
        $keyMap = [
            'id' => 'id',
            'code' => 'code',
            'name' => 'name',
            'level_dict_code' => 'level_dict_code'

        ];
        return $this->success(ResponseFormatter::formatterPaginator($addressPaginator, $keyMap));
    }
}
